<?php
class User_Model extends CI_Model{
	
    function _construct(){
		parent::_construct();
	}
	
	function create($username, $password, $firstname, $lastname, $email, $tel, $address, $languaje, $country){
		$data = array(
		   	'username' 	=> $username,
		   	'password'	=> md5($password),
		   	'firstname' => $firstname,
			'lastname' 	=> $lastname,
			'email' 	=> $email,
			'tel' 		=> $tel,
			'address' 	=> $address,
			'languaje'	=>	$languaje,
			'country'	=>	$country
		);
		$this->db->set('creation_date', 'NOW()', FALSE); 
		$this->db->insert('users', $data);
		
		if($this->db->_error_message() != ''){
	   		$result=null;		
	   	}else{
	   		$result = $this->db->insert_id();
	   	}
	   	return  $result;
	}
	
	function update($id, $username, $password, $firstname, $lastname, $email, $tel, $address, $languaje, $country){
		$data = array(
		   	'username' 	=> $username,
		   	'password'	=> md5($password),
		   	'firstname' => $firstname,
			'lastname' 	=> $lastname,
			'email' 	=> $email,
			'tel' 		=> $tel,
			'address' 	=> $address,
			'languaje'	=>	$languaje,
			'country'	=>	$country
		);
		
		$this->db->where('id', $id);
		
	  	$this->db->update('users', $user);
	   
	   	if($this->db->_error_message() != ''){
	   		$result=null;	
	   	}else{
	   		$result = $id;
	   	}
	   	return  $result;
	}
	
	function get_users($sidx, $sord, $start , $limit) {
		$this->db->order_by($sidx, $sord);
		$this->db->limit($limit, $start);
		$this->db->select('id, username, firstname, lastname, email');
		$query = $this->db->get('users');
		return $query->result_array(); 
	}
	
	function get_users_count() {
		return $this->db->count_all_results('users');
	}
	function get($user){
    	$query=$this->db->get_where('users', array('id' => $id));
     	return $query->result_array();
    }
	function remove ($user){
    	$this->db->delete('users', array('id' => $user)); 
    }
    
	function get_like_users($term) {
		$this->db->like('username', $term, 'after');
		$this->db->select('id, username');
		$query = $this->db->get('users');
		return $query->result_array();  
	}
	
	function check_login($username , $password){
		$pass1= md5($password);
		$query= "SELECT id, password FROM users WHERE username = ? AND password = ?";
		$result= $this->db->query($query,array($username,$pass1));
		if ($result->num_rows() == 1){
			$result=$result->row();
			return $result->id;
		}else{
			return FALSE;
		}
	}
}
